public class Leetcode717 {

    public static void main(String[] args) {
        System.out.println(isOneBitCharacter(new int[]{1, 0, 0}));
        System.out.println(isOneBitCharacter(new int[]{1, 1, 1, 0}));
    }

    public static boolean isOneBitCharacter(int[] bits) {
        if (bits.length == 1 || bits[bits.length - 2] == 0) {
            return true;
        }

        boolean count = false;
        int oneNumber = 0;
        for (int i = bits.length - 2; i >= 0; i--) {
            if (bits[i] == 1 && !count) {
                count = true;
            }

            if (bits[i] == 0 && count) {
                break;
            }

            if (count) {
                oneNumber++;
            }
        }

        return oneNumber % 2 == 0;
    }
}
